function [savings_target6,savings_target7,savings_target6_sample,savings_target7_sample,scaled_att_sample_wave_weighted,scaled_att_target_wave] = generate_external_validity_table_cubic_all_specifications(sample,~,s_cost,tcost,bsimax,SMC,savedir,fbase,coef)
% This function summarizes the savings from applying EWM cubic rules across
% waves. 

% Inputs: 
% (1) sample: cross-sectional dataset with electricity consumption, 
% covariates, and propensity scores
% (2) scost: string representing the cost specification
% (3) tcost: private marginal cost of implementing the program per household
%  >0 represents cost savings; =0 represents kWh reduction 
% (4) bsimax: number of bootstrap runs
% (5) SMC: =1 use social marginal cost; =0 use retail electricity price
% (6) savedir: directory to save the savings table 
% (7) fbase: string used to indicate the propensity score and baseline
% months specification for output table 
% (8) coef: ='delta' if summarizing EWM savings- RCT benchmark, ='ewm' if
% summarizing EWM savings

% Outputs: 
% (1) savings_target6: savings on target wave 6
% (2) savings_target7: savings on target wave 7
% (3) savings_target6_sample: savings on sample wave 3
% (4) savings_target7_sample: savings on sample wave 6
% (5) scaled_att_sample_wave_weighted: scaled att on the weighted sample wave
% (6) scaled_att_target_wave: scaled att on the target wave

%% Calculate EWM cubic savings 
covariates={'income' 'size' 'vintage'};
sample_wave={3 6};
target_wave={6 7};
ev_savings=cell(2,3);
ev_savings_sample=cell(2,3);
scaled_att_sample_wave_weighted=cell(2,3);
scaled_att_target_wave=cell(2,3);
s_winter='';

for i=1:2
    for j=1:3
s_wave=sprintf('%d%d',sample_wave{i},target_wave{i});
filename_coefs=sprintf('coef_ev_cubic_%s_%s%s_%s_weighted.mat',covariates{j},s_cost,s_winter,s_wave);
filename = sprintf('%s_%s',fbase,filename_coefs);
S=load(filename);
ev_savings{i,j}=nanmean(S.g_target_wave.*S.in_Ghat)*S.Yscale_target_wave;
in_Ghat_sample=(S.Xr_sample_wave*S.beta>0);
ev_savings_sample{i,j}=nanmean(S.gr_sample_wave.*S.dr_hh.*in_Ghat_sample)*S.Yscale_sample_wave;
scaled_att_sample_wave_weighted{i,j}=round(S.scaled_att_sample_wave_weighted,2);
scaled_att_target_wave{i,j}=round(S.scaled_att_target_wave,2);
    end
end

if coef=="delta"
    savings_ev_cubic_income_36=ev_savings{1,1}-scaled_att_target_wave{1,1};
    savings_ev_cubic_income_67=ev_savings{2,1}-scaled_att_target_wave{2,1}; 

    savings_ev_cubic_size_36=ev_savings{1,2}-scaled_att_target_wave{1,2};
    savings_ev_cubic_size_67=ev_savings{2,2}-scaled_att_target_wave{2,2}; 

    savings_ev_cubic_vintage_36=ev_savings{1,3}-scaled_att_target_wave{1,3};
    savings_ev_cubic_vintage_67=ev_savings{2,3}-scaled_att_target_wave{2,3}; 

    sample_savings_ev_cubic_income_36=ev_savings_sample{1,1}-scaled_att_sample_wave_weighted{1,1};
    sample_savings_ev_cubic_income_67=ev_savings_sample{2,1}-scaled_att_sample_wave_weighted{2,1}; 

    sample_savings_ev_cubic_size_36=ev_savings_sample{1,2}-scaled_att_sample_wave_weighted{1,2};
    sample_savings_ev_cubic_size_67=ev_savings_sample{2,2}-scaled_att_sample_wave_weighted{2,2}; 

    sample_savings_ev_cubic_vintage_36=ev_savings_sample{1,3}-scaled_att_sample_wave_weighted{1,3};
    sample_savings_ev_cubic_vintage_67=ev_savings_sample{2,3}-scaled_att_sample_wave_weighted{2,3}; 
elseif coef=="ewm"
    savings_ev_cubic_income_36=ev_savings{1,1};
    savings_ev_cubic_income_67=ev_savings{2,1}; 

    savings_ev_cubic_size_36=ev_savings{1,2};
    savings_ev_cubic_size_67=ev_savings{2,2}; 

    savings_ev_cubic_vintage_36=ev_savings{1,3};
    savings_ev_cubic_vintage_67=ev_savings{2,3}; 

    sample_savings_ev_cubic_income_36=ev_savings_sample{1,1};
    sample_savings_ev_cubic_income_67=ev_savings_sample{2,1}; 

    sample_savings_ev_cubic_size_36=ev_savings_sample{1,2};
    sample_savings_ev_cubic_size_67=ev_savings_sample{2,2}; 

    sample_savings_ev_cubic_vintage_36=ev_savings_sample{1,3};
    sample_savings_ev_cubic_vintage_67=ev_savings_sample{2,3}; 
end

    
%% Summarize savings 

savings_target6=[savings_ev_cubic_income_36;savings_ev_cubic_size_36;savings_ev_cubic_vintage_36];
savings_target7=[savings_ev_cubic_income_67;savings_ev_cubic_size_67;savings_ev_cubic_vintage_67];

savings_target6_sample=[sample_savings_ev_cubic_income_36;sample_savings_ev_cubic_size_36;sample_savings_ev_cubic_vintage_36];
savings_target7_sample=[sample_savings_ev_cubic_income_67;sample_savings_ev_cubic_size_67;sample_savings_ev_cubic_vintage_67];

end
